using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WinMenu : MenuModel {

    private Transform _star1;
    private Transform _star2;
    private Transform _star3;
    private HUDButton _btnQuit;
    private HUDButton _btnReplay;
    private HUDButton _btnNext;
    private TextMesh _txtStar1;
    private TextMesh _txtStar2;
    private TextMesh _txtStar3;
    private TextMesh _txtTitle;
    private TextMesh _txtNbEnemyAlive;
    private string nextLevel = "";

    void Start()
    {
        AffectStars();
        AffectButtons();
        AffectTexts();

        int nbStar;
        float time;
        float timeStar1;
        float timeStar2;
        float timeStar3;
        

        PlayerManager.Instance.GetLastLevelResult(out nbStar, out time, out timeStar3, out timeStar2, out timeStar1, out nextLevel);
        
        if (nbStar < 3)
            _star3.renderer.enabled = false;
        if (nbStar < 2)
            _star2.renderer.enabled = false;
        if (nbStar < 1)
            _star1.renderer.enabled = false;

        _txtStar1.text = timeStar1.ToString();
        _txtStar2.text = timeStar2.ToString();
        _txtStar3.text = timeStar3.ToString();

        _txtNbEnemyAlive.text = TextManager.Instance().GetText("txtnbEnemys") + " " + Mathf.Round(time).ToString();

        if (nbStar == 3)
            _txtTitle.text = TextManager.Instance().GetText("txtWin");
        else if (nbStar == 2)
            _txtTitle.text = TextManager.Instance().GetText("txt2Star");
        else if (nbStar == 1)
            _txtTitle.text = TextManager.Instance().GetText("txt1Star");
        else if (nbStar == 0)
            _txtTitle.text = TextManager.Instance().GetText("txtLoose");
        
    }

    //Get Stars
    void AffectStars()
    {
        List<Transform> listStars = new List<Transform>();
        int nbStar = 0;
        foreach (Transform texture in _listTextures)
        {
            if (texture.name == "star")
            {
                listStars.Add(texture);
                if (nbStar == 0)
                    _star1 = texture;
                else if (nbStar == 1)
                    _star2 = texture;
                else
                    _star3 = texture;
                nbStar++;
            }
        }
    }

    //Get Buttons
    void AffectButtons()
    {
        foreach (HUDButton button in _listButtons)
        {

            if (button.name == "quitBtn")
            {
                _btnQuit = button;
            }
            else if (button.name == "replayBtn")
            {
                _btnReplay = button;
            }   
            else if (button.name == "nextBtn")
            {
                _btnNext = button;
            }

        }
    }

    //Get Texts
    void AffectTexts()
    {
        foreach (Transform t in _listTexts)
        {
            TextMesh txt = t.GetComponent<TextMesh>();

            if (t.name == "title")
            {
                _txtTitle = txt;
            }
            else if (t.name == "enemyAlive")
            {
                _txtNbEnemyAlive = txt;
            }
            else if (t.name == "time1")
            {
                _txtStar1 = txt;
            }
            else if (t.name == "time2")
            {
                _txtStar2 = txt;
            }
            else if (t.name == "time3")
            {
                _txtStar3 = txt;
            }
            txt.anchor = TextAnchor.MiddleCenter;
        }
    }

    public override void OnRelease(object o)
    {

        //Check release button
        foreach (HUDButton button in _listButtons)
        {
            if (button == o)
            {
                if (button == _btnNext)
                {
                    if(nextLevel == "")
                        SwitchSceneManager.Instance.SwitchToScene("MenuPrincipal");
                    else
                        SwitchSceneManager.Instance.SwitchToScene(nextLevel);
                }
                    
                else if (button == _btnReplay)
                    SwitchSceneManager.Instance.SwitchToScene(PlayerManager.Instance.GetLastLevelName());
                else if (button == _btnQuit)
                    SwitchSceneManager.Instance.SwitchToScene("MenuPrincipal");
            }
        }

    }
}
